/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sunsite.dao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sunsite.po.Datadic;
import sunsite.po.DatadicType;

/**
 *
 * @author Ruby
 */
public class DatadicDaoImpl extends HibernateDaoSupport implements DatadicDao{

    public List<DatadicType> getAllDatadicTypes()  throws Exception{
        return getHibernateTemplate().find("from DatadicType");
    }

    public DatadicType getDatadicType(int typeId)  throws Exception{
        return (DatadicType)getHibernateTemplate().get(DatadicType.class, typeId);
    }

    public DatadicType getDatadicType(final String typeName) throws Exception {
        List list = (List)getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from DatadicType dt where dt.dictypeName = :typeName").setParameter("typeName", typeName).list();
            }
        });
        if(list!=null)
            return (DatadicType)list.get(0);
        else
            return null;
    }

    public List<Datadic> getDatadicValues(final int typeId)  throws Exception{
        return (List<Datadic>)getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from Datadic d where d.datadicType.dictypeId = :typeId").setParameter("typeId", typeId).list();
            }
        });
    }

    public List<Datadic> getDatadicValues(final String typeName) throws Exception{
        return getDatadicValues(getDatadicType(typeName).getDictypeId());
    }

}
